package com.sls.game.parser.xingqitian;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sls.game.pojo.xingqitian.XingOrderStatusResMsgInfo;

/**
 * 
 * @description 订单查询结果返回解析
 * @author Wangxuegang
 * @date 2012-12-27
 */
public class ParseOrderStatusResMsg {

	private static Logger log = Logger.getLogger(ParseOrderStatusResMsg.class);

	/**
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static XingOrderStatusResMsgInfo parseMsgOfOrderStatus(String xmlStr) {
		XingOrderStatusResMsgInfo ordStaInfo = new XingOrderStatusResMsgInfo();
		InputSource source = null;
		StringReader read = null;
		read = new StringReader(xmlStr);
		source = new InputSource(read);

		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(source);
			Element element = document.getDocumentElement();
			NodeList itemNodes = element.getElementsByTagName("item");

			Element itemElement0 = (Element) itemNodes.item(0);
			String state = itemElement0.getAttribute("name");
			String stateValue = itemElement0.getAttribute("value");
			if ("errcode".equals(state)) {
				ordStaInfo.setErrcode(state);
				ordStaInfo.setErrcodeValue(stateValue);
			} else {
				ordStaInfo.setState(state);
				ordStaInfo.setStateValue(stateValue);
			}

			Element itemElement1 = (Element) itemNodes.item(1);
			if (itemElement1 != null) {
				String exchange_no = itemElement1.getAttribute("name");
				String exchange_noValue = itemElement1.getAttribute("value");
				if (StringUtils.isNotEmpty(exchange_noValue)) {
					exchange_noValue = exchange_noValue.trim();
				}
				if ("errcode".equals(state)) {
					ordStaInfo.setErrmsg(exchange_no);
					ordStaInfo.setErrmsgValue(exchange_noValue);
				} else {
					ordStaInfo.setExchange_no(exchange_no);
					ordStaInfo.setExchange_noValue(exchange_noValue);
				}
			}

			Element itemElement2 = (Element) itemNodes.item(2);
			if (itemElement2 != null) {
				String tranid = itemElement2.getAttribute("name");
				ordStaInfo.setTranid(tranid);
				String tranidValue = itemElement2.getAttribute("value");
				ordStaInfo.setTranidValue(tranidValue);
			}

			Element itemElement3 = (Element) itemNodes.item(3);
			if (itemElement3 != null) {
				String orderMoney = itemElement3.getAttribute("name");
				ordStaInfo.setOrderMoney(orderMoney);
				String orderMoneyValue = itemElement3.getAttribute("value");
				ordStaInfo.setOrderMoneyValue(orderMoneyValue);
			}

			Element itemElement4 = (Element) itemNodes.item(4);
			if (itemElement4 != null) {
				String payMoney = itemElement4.getAttribute("name");
				String payMoneyValue = itemElement4.getAttribute("value");
				if(payMoneyValue.startsWith(".")){
					payMoneyValue = "0" + payMoneyValue;
				}
				ordStaInfo.setPayMoney(payMoney);
				ordStaInfo.setPayMoneyValue(payMoneyValue);
			}
			Element itemElement5 = (Element) itemNodes.item(5);
			if (itemElement5 != null) {
				// String errorCode = itemElement5.getAttribute("name");
				// String errorCodeValue = itemElement5.getAttribute("value");
				// ordStaInfo.setErrcode(errorCode);
				// ordStaInfo.setErrcodeValue(errorCodeValue);
			}
			Element itemElement6 = (Element) itemNodes.item(6);
			if (itemElement6 != null) {
				String remark = itemElement6.getAttribute("name");
				ordStaInfo.setRemark(remark);
				String remarkValue = itemElement6.getAttribute("value");
				ordStaInfo.setRemarkValue(remarkValue);
			}

			Element itemElement7 = (Element) itemNodes.item(7);
			if (itemElement7 != null) {
				String mark = itemElement7.getAttribute("name");
				ordStaInfo.setMark(mark);
				String markValue = itemElement7.getAttribute("value");
				ordStaInfo.setMarkValue(markValue);
			}

		} catch (ParserConfigurationException ep) {
			log.error(ep);
		} catch (SAXException es) {
			log.error(es);
		} catch (IOException ei) {
			log.error(ei);
		} catch (Exception e) {
			log.error(e);
		}

		return ordStaInfo;
	}
	
}
